{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-07-16T14:58:32.321042Z",
     "start_time": "2025-07-16T14:58:31.775867Z"
    }
   },
   "source": "import pandas as pd",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:58:39.481464Z",
     "start_time": "2025-07-16T14:58:34.622482Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. 获取数据\n",
    "order_products = pd.read_csv(\"./data/order_products__prior.csv\")\n",
    "orders = pd.read_csv(\"./data/orders.csv\")\n",
    "products = pd.read_csv(\"./data/products.csv\")\n",
    "aisles = pd.read_csv(\"./data/aisles.csv\")"
   ],
   "id": "ce156b3b819a30f7",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:37:43.928978Z",
     "start_time": "2025-07-16T14:37:43.914975Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 合并表\n",
    "tab1 = pd.merge(aisles, products, on=[\"aisle_id\", \"aisle_id\"])"
   ],
   "id": "6ad6702c12e1f6e2",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:40:02.961859Z",
     "start_time": "2025-07-16T14:39:58.458314Z"
    }
   },
   "cell_type": "code",
   "source": "tab2 = pd.merge(tab1, order_products, on=[\"product_id\", \"product_id\"])",
   "id": "8df9f604b0662192",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:41:29.080156Z",
     "start_time": "2025-07-16T14:41:12.785858Z"
    }
   },
   "cell_type": "code",
   "source": "tab3 = pd.merge(tab2, orders, on=[\"order_id\", \"order_id\"])",
   "id": "c0f3eebda9ddf2d2",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "tab3.head()",
   "id": "cd8835c557c004f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:45:41.393579Z",
     "start_time": "2025-07-16T14:44:47.086193Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3. 找到user_id和aisle之间的关系\n",
    "table = pd.crosstab(tab3[\"user_id\"], tab3[\"aisle\"])"
   ],
   "id": "29ebc80ef2db2406",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:48:06.918003Z",
     "start_time": "2025-07-16T14:48:06.912733Z"
    }
   },
   "cell_type": "code",
   "source": "data = table[:1000]",
   "id": "82bab75cf0d4c1b4",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:48:35.670899Z",
     "start_time": "2025-07-16T14:48:34.688023Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 4. PCA降维\n",
    "from sklearn.decomposition import PCA"
   ],
   "id": "de354d1e1b8fe416",
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:49:42.529248Z",
     "start_time": "2025-07-16T14:49:42.407512Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 实例化一个转换器类\n",
    "transfer = PCA(n_components=0.95)\n",
    "\n",
    "# 调用fit_transform\n",
    "data_new = transfer.fit_transform(data)"
   ],
   "id": "699b5cbf22659e7a",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-16T14:50:01.786440Z",
     "start_time": "2025-07-16T14:50:01.779200Z"
    }
   },
   "cell_type": "code",
   "source": "data_new.shape",
   "id": "defc3293e3da69f0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 35)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "d8523c0e6a73a194"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
